<template>
  <div class="dialogCom">
    <el-dialog
      :visible.sync="isShow"
      title="编辑部门"
      width="50%"
      @close="closeEvent"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model.trim="form.name" autofocus />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model.trim="form.code" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model.trim="form.manager" @focus="selectFocus">
            <el-option
              v-for="item in managerList"
              :key="item.id"
              :value="item.username"
              :label="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model.trim="form.introduce" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="form_btn">
          <el-button @click="isShow = false">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {
  userSimpleGet,
  companyDepartmentPost,
  companyDepartmentPut
} from '@/api/departments'
export default {
  name: 'DialogCom',
  props: {
    init: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      mode: 'add',
      item: {},
      isShow: false,
      managerList: [],
      form: {
        name: '', //	string	非必须		部门名称
        code: '', //	string	非必须		部门编码，同级部门不可重复
        manager: '', //	string	非必须		负责人名称
        introduce: '', //	string	非必须		介绍
        pid: '' //	string	非必须		父级部门ID
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const res = this.init.filter(
                (item) =>
                  item.pid ===
                  (this.mode === 'add' ? this.item.id : this.item.pid)
              )
              res.some(
                (item) =>
                  item.name === value &&
                  (this.mode === 'add' ? true : item.code !== this.item.code)
              )
                ? callback(new Error('已有该部门'))
                : callback()
            },
            trigger: 'change'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              this.init.some(
                (item) =>
                  item.code === value &&
                  (this.mode === 'add' ? true : item.code !== this.item.code)
              )
                ? callback(new Error('部门编码重复'))
                : callback()
            },
            trigger: 'change'
          }
        ],
        manager: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },

  mounted() {
    this.$bus.$on('isShow', (bol, item, mode = 'add') => {
      this.isShow = bol
      this.item = item
      this.form.pid = item.id
      this.mode = mode
      if (mode === 'edit') this.form = { ...item }
    })
  },
  beforeDestroy() {
    this.$bus.$off('isShow')
  },
  methods: {
    closeEvent() {
      this.form = {
        name: '', //	string	非必须		部门名称
        code: '', //	string	非必须		部门编码，同级部门不可重复
        manager: '', //	string	非必须		负责人名称
        introduce: '', //	string	非必须		介绍
        pid: '' //	string	非必须		父级部门ID
      }
      this.$refs.form.resetFields()
    },
    async selectFocus() {
      if (this.managerList.length === 0) {
        const res = await userSimpleGet()
        this.managerList = res.data
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await companyDepartmentPost(this.form)
            this.$message.success('新增成功')
          } else {
            await companyDepartmentPut(this.form)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          this.$emit('getdata')
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.dialogCom {
  .form_btn {
    text-align: center;
    padding-bottom: 20px;
    .el-button {
      margin-right: 20px;
      padding: 10px 20px;
    }
  }
}
</style>
